library Wave16c initializer init requires LegacySystem

	globals
		private trigger TitanPingTrigger = null
		private item array TitanNukeItems
	endglobals

	
	
    private function TitanPing takes nothing returns nothing
		local integer i = 0
		local integer deployed = 0 

        if udg_TitanDeployed[1]==false then
            call PingMinimapEx(13000.0,9000.0,2.0,0,255,0,false) // airfield
		else
			set deployed = deployed + 1
        endif
		
        if udg_TitanDeployed[2]==false then
            call PingMinimapEx(13750.0,-5400.0,2.0,0,255,0,false) // power gen
		else
			set deployed = deployed + 1
        endif
		
        if udg_TitanDeployed[3]==false then
            call PingMinimapEx(-1000.0,-500.0,2.0,0,255,0,false) // town
		else
			set deployed = deployed + 1
        endif
		
        if udg_TitanDeployed[4]==false then
            call PingMinimapEx(-6666.0,14500.0,2.0,0,255,0,false) // windmill
		else
			set deployed = deployed + 1
        endif

		if deployed < 4 then
			call TriggerSleepAction(3.0)
			loop
				exitwhen i > 5
				if TitanNukeItems[i] != null and not IsItemOwned(TitanNukeItems[i]) then
					if  GetWidgetLife( TitanNukeItems[i]) > 0.405 then
						call PingMinimapEx(GetItemX(TitanNukeItems[i]), GetItemY(TitanNukeItems[i]), 30.0, 35, 35, 255, false)
					else
						set TitanNukeItems[i] = null
					endif
				endif
				set i = i + 1
			endloop		
		endif

    endfunction

    private function TitanDeploymentConditions takes nothing returns boolean
        return GetSpellAbilityId()=='A08J' //Deploy Titan Warhead
    endfunction

    private function TitanDeployment takes nothing returns nothing
        local integer i = 1
        local unit u = GetTriggerUnit()
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local real dx
        local real dy
        local integer deployment = 0
        local boolean titanvalid = false
		local item nuke
        set dx = GetRectCenterX(gg_rct_Airfield)-x
        set dy = GetRectCenterY(gg_rct_Airfield)-y
        if dx*dx+dy*dy<=810000.0 and udg_TitanDeployed[1]==false then //900 distance
            set titanvalid = true
            set udg_TitanDeployed[1] = true
            set deployment = 1
        else
            set dx = GetRectCenterX(gg_rct_PowerGenerator)-x
            set dy = GetRectCenterY(gg_rct_PowerGenerator)-y
            if dx*dx+dy*dy<=360000.0 and udg_TitanDeployed[2]==false then //600 distance
                set titanvalid = true
                set udg_TitanDeployed[2] = true
                set deployment = 2
            else
                set dx = GetRectCenterX(gg_rct_Town)-x
                set dy = GetRectCenterY(gg_rct_Town)-y
                if dx*dx+dy*dy<=360000.0 and udg_TitanDeployed[3]==false then //600 distance
                    set titanvalid = true
                    set udg_TitanDeployed[3] = true
                    set deployment = 3
                else
                    set dx = GetRectCenterX(gg_rct_Home7)-x
                    set dy = GetRectCenterY(gg_rct_Home7)-y
                    if dx*dx+dy*dy<=360000.0 and udg_TitanDeployed[4]==false then //600 distance
                        set titanvalid = true
                        set udg_TitanDeployed[4] = true
                        set deployment = 4
                    endif
                endif
            endif
        endif

        if titanvalid then
            call AttachSoundToUnit(gg_snd_FuelSynthesizer, udg_BlackHawk)
            call SetSoundVolume(gg_snd_FuelSynthesizer, 127)
            call StartSound(gg_snd_FuelSynthesizer)
			
			set nuke = GetUnitItem(u,'I01A')
			
			set i = 0
			loop
				exitwhen i > 5
				
				if nuke == TitanNukeItems[i] then
					set TitanNukeItems[i] = null
					exitwhen true
				endif
				
				set i = i + 1
			endloop
            call RemoveItem(nuke)

            if deployment==1 then
                call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 10.0, "Titan TacNuke Deployed at Jefferson Airfield." )
                call PingMinimapEx(GetRectCenterX(gg_rct_Airfield),GetRectCenterY(gg_rct_Airfield),60.0,200,200,0,false)
            elseif deployment==2 then
                call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 10.0, "Titan TacNuke Deployed at Power Generator." )
                call PingMinimapEx(GetRectCenterX(gg_rct_PowerGenerator),GetRectCenterY(gg_rct_PowerGenerator),60.0,200,200,0,false)
            elseif deployment==3 then
                call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 10.0, "Titan TacNuke Deployed at Ravenholm." )
                call PingMinimapEx(GetRectCenterX(gg_rct_Town),GetRectCenterY(gg_rct_Town),60.0,200,200,0,false)
            elseif deployment==4 then
                call DisplayTimedTextToPlayer( GetLocalPlayer(), 0.0, 0.0, 10.0, "Titan TacNuke Deployed at Windmill." )
                call PingMinimapEx(GetRectCenterX(gg_rct_Home7),GetRectCenterY(gg_rct_Home7),60.0,200,200,0,false)
            endif

            if udg_TitanDeployed[1] and udg_TitanDeployed[2] and udg_TitanDeployed[3] and udg_TitanDeployed[4] then
                call DestroyTrigger(GetTriggeringTrigger())
                call putBoolean(null,"NukeSuccess",true)
                call MBSet( udg_SquadStatus, 1, 1, "Objective Completed : All Titan TacNukes Deployed" )
                call ALICE2Text(10.0, "TacNukes Armed. |cff1e90ff50 XP|r awarded to all Marines." )
				set i = 1
                loop
                    exitwhen i>udg_InitialPlayers
                    if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                        set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + 50
                    endif
                    set i=i+1
                endloop
            endif
        endif
        set u = null
    endfunction
	
	private function InitTitanNukes takes nothing returns nothing
		local integer i = 1
		local trigger titan_trigger= CreateTrigger()
		local trigger repeat_trigger = CreateTrigger()
		
	    call TriggerAddCondition(titan_trigger,Condition(function TitanDeploymentConditions))
        call TriggerAddAction(titan_trigger,function TitanDeployment)
        loop
            exitwhen i>udg_InitialPlayers
			call TriggerRegisterPlayerUnitEvent(titan_trigger, ConvertedPlayer(udg_Sorted[i]), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
            set i=i+1
        endloop
		
		call TriggerRegisterTimerEvent(repeat_trigger, 33.0, true)
		call TriggerAddAction(repeat_trigger, function TitanPing)
		set titan_trigger = null
		set repeat_trigger = null
	endfunction
	
    private function Wave16c takes nothing returns nothing
        local location l
        local real x
        local real y
        local real dx
        local real dy
        local integer i=1
        set udg_CurrentWave = 16

        call GenericActions.execute()
        call PolledWait(45.)
        set l = GetRandomLocInRect(bj_mapInitialPlayableArea)
        loop
            set x = GetLocationX(l)
            set y = GetLocationY(l)
            set dx = x-GetUnitX(udg_Residence41)
            set dy = y-GetUnitY(udg_Residence41)
            exitwhen dx*dx+dy*dy>25000000.0 //5000 radius
            call RemoveLocation(l)
            set l = GetRandomLocInRect(bj_mapInitialPlayableArea)
        endloop
        set udg_Boss = CreateUnitAtLoc(Player(13),'U00V',l,0.0) //Havok
        call SuspendHeroXP(udg_Boss,true)
        call RemoveLocation(l)
        call GeneralText(10.0, "|cffcd950cCaptain Benjamin|r : This is C-130 Tango Five Six Delta. We have been damaged and are inbound for an immedia.. " )
        call PolledWait(10.)
        call GeneralText(10.0, "|cffcd950cCWO Nadav|r : Our evac has just been delayed. Tango 5-6-Delta has the TITANs we need to blow this joint. You'll have to get them and place them at the specified locations." )
        call PolledWait(6.)
        call GeneralText(10.0, "|cffcd950cMarine|r : We're the only ones on the ground. Let's move.. " )
        call PolledWait(6.)
        call GeneralText(10.0, "|cffcd950cMarine #2|r : We're going nuclear? " )
        call PolledWait(5.)
        call GeneralText(10.0, "|cffcd950cCWO Nadav|r : Yes, Fox and Golf Companies have already been evacuated. You're the last ones out here." )
        call PolledWait(7.)
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Locate and deploy TITAN TacNukes" )
        call QuestSetDescription(udg_CurrentOrders, "Locate and deploy TITAN TacNukes." )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Locate and deploy TITAN TacNukes." )
        call MissionUpdate.execute()
        call PolledWait(7.)
        call ALICE2Text(5.0, "|cff8b864eALICE|r : Transmitting Titan TacNuke drop sites.." )
		
		call InitTitanNukes()
		set i = 0
        loop
            exitwhen i>4
            set l = GetRandomLocInRect(bj_mapInitialPlayableArea)
            set x = GetLocationX(l)
            set y = GetLocationY(l)
            set TitanNukeItems[i] = CreateItem('I01A',x,y)
            call PingMinimapEx(x, y, 33.0, 35, 35, 255, false)
            call TriggerSleepAction(3.)
            call RemoveLocation(l)
            set i=i+1
        endloop
		
		
        set l = null
        call PolledWait(15.*udg_Pace)
        call GeneralText(10.0, "|cffcd950cBattalion|r : When you get those TacNukes, you've got to deploy them across the Sector. We've marked the deployment zones at your TacMap." )
        call PolledWait(70.*udg_Pace)
        if udg_Players>0 then
            call ExecuteRegisteredFunction("Wave17c")
            //call Wave17c.execute()
        endif
    endfunction

    private function init takes nothing returns nothing
        call RegisterFunction("Wave16c", function Wave16c)
    endfunction

endlibrary